from .base import Base
from sqlalchemy import LargeBinary, Column, ForeignKey, Column, Integer, String, Float, Date, Numeric,JSON, TEXT,SmallInteger
from sqlalchemy.orm import  relationship

class BalanceSheet(Base):
    __tablename__ = 'balance_sheet'
    id = Column(Integer, primary_key=True)
    company_id = Column(Integer, ForeignKey('companies.id'), nullable=False)
    report_date = Column(Date)  # 报告日期

    # 报表核心指标
    total_equity = Column(Numeric)  # 所有者权益（或股东权益）合计
    total_assets = Column(Numeric)  # 资产合计
    total_liabilities = Column(Numeric)  # 负债合计
    equity_attributable_to_parent_company = Column(Numeric)  # 归属于母公司所有者权益合计

    # 报表全部指标 - 流动资产
    current_assets = Column(Numeric)  # 流动资产合计
    cash_and_cash_equivalents = Column(Numeric)  # 货币资金
    trading_financial_assets = Column(Numeric)  # 交易性金融资产
    receivables = Column(Numeric)  # 应收票据及应收账款
    notes_receivable = Column(Numeric)  # 其中：应收票据
    accounts_receivable = Column(Numeric)  # 应收账款
    prepaid_expenses = Column(Numeric)  # 预付款项
    other_receivables = Column(Numeric)  # 其他应收款合计
    inventory = Column(Numeric)  # 存货
    current_assets_expiring_within_one_year = Column(Numeric)  # 一年内到期的非流动资产
    other_current_assets = Column(Numeric)  # 其他流动资产
    total_cash = Column(Numeric)  # 总现金

    company = relationship("Company", back_populates="balance_sheet")

class ProfitStatement(Base):
    __tablename__ = 'profit_statements'
    id = Column(Integer, primary_key=True)
    company_id = Column(Integer, ForeignKey('companies.id'), nullable=False)
    report_date = Column(Date)  # 报告日期

    # 报表核心指标
    net_profit = Column(Numeric)  # 净利润
    operating_total = Column(Numeric)  # 营业总收入
    operating_total_cost = Column(Numeric)  # 营业总成本
    net_profit_attributable_to_parent_company = Column(Numeric)  # 归属于母公司所有者的净利润
    non_recurring_profit_loss = Column(Numeric)  # 扣除非经常性损益后的净利润

    # 报表全部指标
    revenue = Column(Numeric)  # 营业收入
    total_operating_costs = Column(Numeric)  # 营业总成本
    cost_of_goods_sold = Column(Numeric)  # 营业成本
    sales_taxes_and_surcharges = Column(Numeric)  # 营业税金及附加
    selling_expenses = Column(Numeric)  # 销售费用
    administrative_expenses = Column(Numeric)  # 管理费用
    r_and_d_expenses = Column(Numeric)  # 研发费用
    financial_expenses = Column(Numeric)  # 财务费用
    interest_expense = Column(Numeric)  # 利息费用
    interest_income = Column(Numeric)  # 利息收入
    asset_impairment_loss = Column(Numeric)  # 资产减值损失
    credit_impairment_loss = Column(Numeric)  # 信用减值损失

    company = relationship("Company", back_populates="profit_statement")


class FinancialReport(Base):
    __tablename__ = "financial_reports"

    id = Column(Integer, primary_key=True)
    company_id = Column(Integer, ForeignKey('companies.id'), nullable=False)
    title = Column(String)  # 报告标题
    report_type = Column(SmallInteger) # 0:季度报告, 1: 半年度报告, 2:年度报告
    content_type = Column(String) # pdf
    content = Column(LargeBinary) # pdf content
    company = relationship("Company", back_populates="financial_report")